<?php

namespace App\Console\Commands;

use Illuminate\Support\Facades\DB;
use Illuminate\Console\Command;

class DigSiteMap extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature    = 'dig:sitemap';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description  = 'dig:sitemap';
    protected $urls         = [];
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->sitemap('pages', []);
        $this->sitemap('columns', []);
        $this->sitemap('tags', []);
        $this->sitemap('areas', []);
        $this->sitemap('sites', ['state'=>1]);
        $this->sitemap('articles', ['state'=>1]);
        $sitemap = App()->make("sitemap");
        foreach ($this->urls as $v)
        {
            $sitemap->addSitemap($this->urlTo($v[0]));
        }
		// create file sitemap.xml in your public folder (format, filename)
		$sitemap->store('sitemapindex', 'index', 'sitemap');
    }
    
    function sitemap($name, $where)
    {
        $this->info('dig '.$name.' action!');
        $pages      = 3000;
	    $count  = DB::table($name)->count();
	    $num    = ceil($count/$pages);
	    $this->line($name.'=='.$num.'=='.$count);
	    
	    for($i=0; $i<$num; $i++)
        {
            $list   = DB::table($name)->where($where)->orderBy('id', 'desc')->skip($i*$pages)->take($pages)->get();
    		$this->map($list, $name, $i);
    		$this->line($i.'=='.$name);
        }
    }
    
    function urlTo($str)
    {
        if ($str)
        {
            return env('APP_URL').$str;
        } else {
            return env('APP_URL');    
        }
        
    }
    
    function map($list, $name, $i)
    {
        $sitemap    = App()->make("sitemap");
        if ($name == 'tags')
        {
            foreach ($list as $v)
    		{
    		    $sitemap->add($this->urlTo('')."/tags/$v->name", $v->created_at, '1.0', 'daily');
    		}
        }
        if ($name == 'sites')
		{
		    foreach ($list as $v)
    		{
    			$sitemap->add($this->urlTo('')."/siteinfo-$v->id.html", $v->created_at, '1.0', 'daily');
    		}
		}
		if ($name == 'articles')
		{
		    foreach ($list as $v)
    		{
    			$sitemap->add($this->urlTo('')."/artinfo-$v->id.html", $v->created_at, '1.0', 'daily');
    		}
		}
		if ($name == 'areas')
		{
		    foreach ($list as $v)
    		{
    			$sitemap->add($this->urlTo('')."/area/$v->name", $v->created_at, '1.0', 'daily');
    		}
		}
		if ($name == 'columns')
		{
		    foreach ($list as $v)
    		{
    		    if ($v->type == 0)
    		    {
    		        if ($v->parent_id == 0)
    		        {
    		            $sitemap->add($this->urlTo('')."/site/$v->id", $v->created_at, '1.0', 'daily');
    		        } else {
    		            $sitemap->add($this->urlTo('')."/site/$v->id/son/$v->parent_id", $v->created_at, '1.0', 'daily');
    		        }
    		    } else {
    		        $sitemap->add($this->urlTo('')."/article/$v->id", $v->created_at, '1.0', 'daily');
    		    }
    		}
		}
		if ($name == 'pages')
		{
		    foreach ($list as $v)
    		{
    			$sitemap->add($this->urlTo('')."/pages/$v->name.html", $v->created_at, '1.0', 'daily');
    		}
		}
		if ($i == 0)
		{
		    $sitemap->store('xml', $name, 'sitemap');
		    $this->urls[] = ['/storage/sitemap/'.$name.'.xml'];
		} else {
		    $sitemap->store('xml', $name.'_'.$i, 'sitemap'); 
		    $this->urls[] = ['/storage/sitemap/'.$name.'_'.$i.'.xml'];
		}
    }
}
